/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2020 Intel Corporation */

#ifndef _UAPI_LINUX_ASPEED_MCTP_H
#define _UAPI_LINUX_ASPEED_MCTP_H

#include <linux/ioctl.h>
#include <linux/types.h>

/*
 * aspeed-mctp is a simple device driver exposing a read/write interface:
 *  +----------------------+
 *  | PCIe VDM Header      | 16 bytes (Big Endian)
 *  +----------------------+
 *  | MCTP Message Payload | 64/128/256/512 bytes (Big Endian)
 *  +----------------------+
 *
 * MCTP packet description can be found in DMTF DSP0238,
 * MCTP PCIe VDM Transport Specification.
 */

#define ASPEED_MCTP_PCIE_VDM_HDR_SIZE 16

/*
 * uevents generated by aspeed-mctp driver
 */
#define ASPEED_MCTP_READY "PCIE_READY"

/*
 * maximum possible number of struct eid_info elements stored in list
 */
#define ASPEED_MCTP_EID_INFO_MAX 256

/*
 * MCTP operations
 * @ASPEED_MCTP_IOCTL_FILTER_EID: enable/disable filter incoming packets based
 * on Endpoint ID (BROKEN)
 * @ASPEED_MCTP_IOCTL_GET_BDF: read PCI bus/device/function of MCTP Controller
 * @ASPEED_MCTP_IOCTL_GET_MEDIUM_ID: read MCTP physical medium identifier
 * related to PCIe revision
 * @ASPEED_MCTP_IOCTL_GET_MTU: read max transmission unit (in bytes)
 * @ASPEED_MCTP_IOCTL_REGISTER_DEFAULT_HANDLER Register client as default
 * handler that receives all MCTP messages that were not dispatched to other
 * clients
 * @ASPEED_MCTP_IOCTL_REGISTER_TYPE_HANDLER Register client to receive all
 * messages of specified MCTP type or PCI vendor defined type
 * @ASPEED_MCTP_IOCTL_UNREGISTER_TYPE_HANDLER Unregister client as handler
 * for specified MCTP type or PCI vendor defined message type
 * @ASPEED_MCTP_GET_EID_INFO: read list of existing endpoint mappings
 * returns count which is less of the two requested count and existing count
 * @ASPEED_MCTP_SET_EID_INFO: write list of endpoint mappings
 * overwrites already existing endpoint mappings
 */

struct aspeed_mctp_filter_eid {
	__u8 eid;
	bool enable;
};

struct aspeed_mctp_get_bdf {
	__u16 bdf;
};

struct aspeed_mctp_get_medium_id {
	__u8 medium_id;
};

struct aspeed_mctp_get_mtu {
	__u8 mtu;
};

struct aspeed_mctp_type_handler_ioctl {
	__u8 mctp_type; /* MCTP message type as per DSP239*/
	/* Below params must be 0 if mctp_type is not Vendor Defined PCI */
	__u16 pci_vendor_id; /* PCI Vendor ID */
	__u16 vendor_type; /* Vendor specific type */
	__u16 vendor_type_mask; /* Mask applied to vendor type */
};

struct aspeed_mctp_eid_info {
	__u8 eid;
	__u16 bdf;
};

struct aspeed_mctp_get_eid_info {
	__u64 ptr;
	__u16 count;
	__u8 start_eid;
};

struct aspeed_mctp_set_eid_info {
	__u64 ptr;
	__u16 count;
};

#define ASPEED_MCTP_IOCTL_BASE 0x4d

#define ASPEED_MCTP_IOCTL_FILTER_EID                                           \
	_IOW(ASPEED_MCTP_IOCTL_BASE, 0, struct aspeed_mctp_filter_eid)
#define ASPEED_MCTP_IOCTL_GET_BDF                                              \
	_IOR(ASPEED_MCTP_IOCTL_BASE, 1, struct aspeed_mctp_get_bdf)
#define ASPEED_MCTP_IOCTL_GET_MEDIUM_ID                                        \
	_IOR(ASPEED_MCTP_IOCTL_BASE, 2, struct aspeed_mctp_get_medium_id)
#define ASPEED_MCTP_IOCTL_GET_MTU                                              \
	_IOR(ASPEED_MCTP_IOCTL_BASE, 3, struct aspeed_mctp_get_mtu)
#define ASPEED_MCTP_IOCTL_REGISTER_DEFAULT_HANDLER                             \
	_IO(ASPEED_MCTP_IOCTL_BASE, 4)
#define ASPEED_MCTP_IOCTL_REGISTER_TYPE_HANDLER                                \
	_IOW(ASPEED_MCTP_IOCTL_BASE, 6, struct aspeed_mctp_type_handler_ioctl)
#define ASPEED_MCTP_IOCTL_UNREGISTER_TYPE_HANDLER                              \
	_IOW(ASPEED_MCTP_IOCTL_BASE, 7, struct aspeed_mctp_type_handler_ioctl)
#define ASPEED_MCTP_IOCTL_GET_EID_INFO                                         \
	_IOWR(ASPEED_MCTP_IOCTL_BASE, 8, struct aspeed_mctp_get_eid_info)
#define ASPEED_MCTP_IOCTL_SET_EID_INFO                                         \
	_IOW(ASPEED_MCTP_IOCTL_BASE, 9, struct aspeed_mctp_set_eid_info)

#endif /* _UAPI_LINUX_ASPEED_MCTP_H */
